/**
 * Created by Administrator on 2017/6/28.
 */
var mongoose = require('../db/db');
var CategorySchema = require('../schemas/category');

CategorySchema.static('findByLimit', function (callback) {
  return this.find({}).sort({time: 1}).populate({path: 'movies', options: {limit: 4, sort: {time: -1}}}).exec(callback);
});

CategorySchema.static('findById', function (id, callback) {
  return this.findOne({_id: id}).populate({path: 'movies', options: {sort: {time: -1}}}).exec(callback);
});

CategorySchema.static('findByIdAndPages', function (id, pageSize, page, callback) {
  var skipNum = (page - 1) * pageSize;
  return this.findOne({_id: id}).populate({path: 'movies', options: {skip: skipNum, sort: {time: -1}, limit: pageSize}}).exec(callback);
});

CategorySchema.static('findAll', function (id, callback) {
  return this.find().sort({time: 1}).populate('movies').exec(callback);
});

module.exports = mongoose.model('Category', CategorySchema);